extends Control


@onready var nameEdit:LineEdit = $NameEdit
var loginResultTargetScene = ""

func _ready():
	GlobalSignals.NM_LOGIN_RESULT.connect(NM_LOGIN_RESULT_handler)
	nameEdit.text_changed.connect(textChangeHandler)
	
	if Global.localUserName != "":
		nameEdit.text = Global.localUserName
	

func _exit_tree():
	GlobalSignals.NM_LOGIN_RESULT.disconnect(NM_LOGIN_RESULT_handler)
	nameEdit.text_changed.disconnect(textChangeHandler)


func NM_LOGIN_RESULT_handler(data):
	Global.currentUser = data["user"]
	Global.changeScene(loginResultTargetScene)


func textChangeHandler(t:String):
	Global.localUserName = t.strip_edges()


func _on_single_pressed():
	GlobalSignals.PLAY_SOUND.emit(SoundManager.CLICK)
	
	if Global.localUserName == "":
		Toast.showMessage("请输入有效的用户名称")
		return
	
	loginResultTargetScene = "MainGameSinglePlayer"
	NetMessages.userLogin(Global.localUserName)


func _on_button_2_pressed():
	GlobalSignals.PLAY_SOUND.emit(SoundManager.CLICK)
	
	if Global.localUserName == "":
		Toast.showMessage("请输入有效的用户名称")
		return
		
	loginResultTargetScene = "RoomList"
	NetMessages.userLogin(Global.localUserName)


func _on_rank_button_pressed():
	GlobalSignals.PLAY_SOUND.emit(SoundManager.CLICK)
	Global.changeScene("Rank")


func _on_exit_game_pressed():
	GlobalSignals.PLAY_SOUND.emit(SoundManager.CLICK)
	Global.exitGame()
